#include "startscene.h"
#include "common.h"
#include "selectitemview.h"
#include "loadscene.h"
#include "createrole.h"

using namespace cocos2d;

namespace fireplane
{
	CCScene* StartScene::scene()
	{
		CCScene * scene = NULL;
		do 
		{
			// 'scene' is an autorelease object
			scene = CCScene::create();
			CC_BREAK_IF(! scene);

			CCNode* parentNode = Common::createScaleNode(scene);
			// 'layer' is an autorelease object
			StartScene *layer = StartScene::node();
			CC_BREAK_IF(! layer);

			// add layer as a child to scene
			parentNode->addChild(layer);
		} while (0);

		// return the scene
		return scene;
	}

	void StartScene::_createStartBg()
	{
		CCSize winSize = Common::getWinSize();
		CCSprite* bg = CCSprite::create("start/UI_1_1.png");
		bg->setPosition(ccp(winSize.width / 2,winSize.height / 2));
		addChild(bg,SL_BG);

		CCAction* zoomOut = CCScaleTo::create(2.5f,1.1f,1.1f);
		CCAction* zoomIn = CCScaleTo::create(2.5f,1.0f,1.0f);

		CCAction* action = CCSequence::create((CCFiniteTimeAction*)zoomOut,
			zoomIn,NULL);
		action = CCRepeatForever::create((CCActionInterval*)action);
		bg->runAction(action);
	}

	void StartScene::_createArrows()
	{
		float move_x = 276.0f + 60;
		float move_y = 56;

		CCSpriteFrame* frame =
			CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("UI_1_2.png");
		_left = CCSprite::createWithSpriteFrame(frame);
		_left->setPosition(ccp(10 + _left->getContentSize().width / 2,50));
		_left->setAnchorPoint(ccp(0.5f,0));

		{
			CCPoint orgpos = _left->getPosition();
			CCPoint despos = orgpos;
			despos.x -= 20;

			CCAction* m0 = CCMoveTo::create(0.5f,despos);
			CCAction* m1 = CCMoveTo::create(0.5f,orgpos);

			_left->runAction(CCRepeatForever::create((CCActionInterval*)CCSequence::create((CCFiniteTimeAction*)m0,m1,NULL)));
		}
	
		addChild(_left);

		frame =
			CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(
			"UI_1_3.png");

		_right = CCSprite::createWithSpriteFrame(frame);
		_right->setPosition(ccp(_left->getPositionX() + move_x + _right->getContentSize().width / 2 - 40,
			_left->getPositionY() + move_y));
		_right->setAnchorPoint(ccp(0.5f,0));

		{
			CCPoint orgpos = _right->getPosition();
			CCPoint despos = orgpos;
			despos.x += 20;

			CCAction* m0 = CCMoveTo::create(0.5f,despos);
			CCAction* m1 = CCMoveTo::create(0.5f,orgpos);

			_right->runAction(CCRepeatForever::create((CCActionInterval*)CCSequence::create((CCFiniteTimeAction*)m0,m1,NULL)));
		}

		addChild(_right);
	}

	void StartScene::_createLogo()
	{
		CCSize winSize = Common::getWinSize();
		CCSprite* logo = CCSprite::create("start/title.png");

		logo->setPosition(ccp(winSize.width / 2,winSize.height - logo->getContentSize().height / 2));
		addChild(logo);
	}

	bool StartScene::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
	{
		bool ret = _selectView->ccTouchBegan(pTouch,pEvent);

		if (ret)
		{
			_FocusByWho = FBW_SelectItemView;
			return true;
		}
		

		return false;
	}

	void StartScene::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
	{

	}

	void StartScene::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
	{
		if(_FocusByWho == FBW_SelectItemView)
			_selectView->ccTouchEnded(pTouch,pEvent);

		_FocusByWho = FBW_not;
	}

	void StartScene::registerWithTouchDispatcher()
	{
		CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,-128 + 1,true);
	}
	// on "init" you need to initialize your instance
	bool StartScene::init()
	{
		bool bRet = false;
		do 
		{
			CC_BREAK_IF(! CCLayer::init());
			_createStartBg();
			_createArrows();
			_createMenuItem();
			_createLogo();

			_FocusByWho = FBW_not;
			setTouchEnabled(true);
			bRet = true;
		} while (0);

		return bRet;
	}


	static int _load_idx = 0;
	static const char* const _all_load_res[] = 
	{
		"start/title.png",
		"start/startscene.png",
		"start/UI_1_1.png",
		"start/startscene.plist",
	};


	bool StartScene::loadRes(void)
	{
		int count = sizeof(_all_load_res) / sizeof(_all_load_res[0]);

		if (_load_idx == count)
		{
			CCDirector::sharedDirector()->replaceScene(StartScene::scene());
			_load_idx = 0;
			return true;
		}
		else
		{
			string fileName = _all_load_res[_load_idx];

			Common::doLoadLogic(fileName);

			_load_idx++;
			return false;
		}
	}


	static const char* _all_item_file_name[] = 
	{
		"startfire.png",
		"enterlibrary.png",
		"skill.png",
		"rongyuxunzang.png",
		"yanfazhongxin.png",
		"setting.png",
	};

	void StartScene::_createMenuItem()
	{
		_selectView = new SelectItemView();
		int size = sizeof(_all_item_file_name) / sizeof(_all_item_file_name[0]);
		CCPoint pos = ccp(_left->getPositionX() + _left->getContentSize().width - 20,
			_left->getPositionY());

		_selectView->init((char**)_all_item_file_name,size,pos,this);
		addChild(_selectView);
		_selectView->autorelease();
	}

	void StartScene::onClickItem( int tag )
	{
		if (tag == 0)
		{
			LoadingScene::registerLoadFunction(&(CreateRole::loadRes));
			CCScene *pScene = LoadingScene::scene();
			CCDirector::sharedDirector()->replaceScene(pScene);
		}
	}
}


